﻿#include "Yimoogi.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/Data/Stat.h"
#include "../../../Shared/ServerPackets.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../../Settings.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    Server::MirObjects::MapObject *Yimoogi::getTarget() const
    {
        return _target;
    }

    void Yimoogi::setTarget(MapObject *value)
    {
        if (_target == value)
        {
            return;
        }
        _target = value;
        if (SisterMobReady())
        {
            SisterMob->setTarget(value);
        }

        NoAttack = value == nullptr ? true : false;
    }

    Yimoogi::Yimoogi(MonsterInfo *info) : MonsterObject(info)
    {
        SpawnTime = getEnvir()->getTime() + 4000;
        NoAttack = true;
    }

    bool Yimoogi::SisterMobReady()
    {
        if (SisterMob == nullptr)
        {
            return false;
        }
        if (SisterMob->Dead)
        {
            return false;
        }

        return true;
    }

    bool Yimoogi::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 2 || y > 2)
        {
            return false;
        }

        return (x <= 1 && y <= 1) || (x == y || x % 2 == y % 2);
    }

    bool Yimoogi::InRangedAttackRange(int distance)
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), distance);
    }

    void Yimoogi::FindTarget()
    {
        if (!NoAttack)
        {
            MonsterObject::FindTarget();
        }
    }

    void Yimoogi::Attack()
    {
        if (!getTarget()->IsAttackTarget(this) || NoAttack)
        {
            setTarget(nullptr);
            return;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !InAttackRange();

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (!ranged && getEnvir()->Random->Next(5) > 0)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::MACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
        else
        {
            AttackTime = getEnvir()->getTime() + AttackSpeed + 500;
            if (damage == 0)
            {
                return;
            }

            if (InRangedAttackRange(PoisonAttackRange) && getEnvir()->Random->Next(6) == 0)
            {
                S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->Type = 1;
                Broadcast(tempVar2);

                PoisonTarget(getTarget(), 1, 6, PoisonType::Red, 2000);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectRangeAttack *tempVar3 = new S::ObjectRangeAttack();
                tempVar3->ObjectID = ObjectID;
                tempVar3->Direction = getDirection();
                tempVar3->Location = getCurrentLocation();
                tempVar3->TargetID = getTarget()->ObjectID;
                Broadcast(tempVar3);

                DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::MAC});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
            }
        }


        if (getTarget()->Dead)
        {
            FindTarget();
        }

    }

    void Yimoogi::ProcessAI()
    {
        if (Dead)
        {
            return;
        }

        if (!IsChild && HP <= Stats[Stat::HP] / 10 && !FinalTeleport)
        {
            Point *teleportlocation = getCurrentLocation();
            if (TeleportRandom(40, 0))
            {
                FinalTeleport = true;

                MonsterObject *mob = nullptr;
                for (int i = 0; i < WhiteSnakeCount; i++)
                {
                    mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::WhiteSnake));
                    if (mob == nullptr)
                    {
                        continue;
                    }

                    if (!mob->Spawn(getCurrentMap(), teleportlocation))
                    {
                        continue;
                    }

                    mob->setTarget(getTarget());
                    mob->ActionTime = getEnvir()->getTime() + 2000;
                }
                setTarget(nullptr);
            }
        }

        if (!IsChild && !ChildSpawned && getEnvir()->getTime() > SpawnTime)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->Type = 2;
            Broadcast(tempVar);
            SpawnSlave();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            return;
        }

        if (IsChild && SisterMobReady())
        {
            if (Functions::InRange(getCurrentLocation(), SisterMob->getCurrentLocation(), 2) && getTarget() == nullptr && Functions::MaxDistance(getCurrentLocation(), SisterMob->getCurrentLocation()) < 10)
            {
                MoveTo(SisterMob->getCurrentLocation());
                return;
            }
        }

        MonsterObject::ProcessAI();
    }

    void Yimoogi::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }
        if (InAttackRange() && !getCanAttack())
        {
            return;
        }

        if (InRangedAttackRange(AttackRange) && getCanAttack())
        {
            Attack();
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }

    void Yimoogi::SpawnSlave()
    {
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        MonsterObject *mob = GetMonster(getEnvir()->GetMonsterInfo(Info->Name));

        if (mob == nullptr)
        {
            MonsterObject::ProcessAI();
            return;
        }

        Yimoogi *childmob = static_cast<Yimoogi*>(mob);

        if (!childmob->Spawn(getCurrentMap(), getFront()))
        {
           childmob->Spawn(getCurrentMap(), getCurrentLocation());
        }

        if (getTarget() != nullptr)
        {
            childmob->setTarget(getTarget());
            childmob->NoAttack = false;
        }

        childmob->IsChild = true;
        childmob->SisterMob = this;
        childmob->ActionTime = getEnvir()->getTime() + 2000;
        SisterMob = childmob;
        ChildSpawned = true;
    }

    bool Yimoogi::TeleportRandom(int attempts, int distance, Map *temp)
    {
        for (int i = 0; i < attempts; i++)
        {
            Point *location;

            if (distance <= 0)
            {
                location = new Point(getEnvir()->Random->Next(getCurrentMap()->Width), getEnvir()->Random->Next(getCurrentMap()->Height));
            }
            else
            {
                location = new Point(getCurrentLocation()->X + getEnvir()->Random->Next(-distance, distance + 1), getCurrentLocation()->Y + getEnvir()->Random->Next(-distance, distance + 1));
            }

            if (Teleport(getCurrentMap(), location, true, 1))
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete location' statement was not added since location was passed to a method or constructor. Handle memory management manually.
                return true;
            }

//C# TO C++ CONVERTER TODO TASK: A 'delete location' statement was not added since location was passed to a method or constructor. Handle memory management manually.
        }

        return false;
    }

    void Yimoogi::Drop()
    {
        if (!SisterMobReady())
        {
            MonsterObject::Drop();
        }
    }
}
